<?php
namespace app\modules\v1\controllers;
use Yii;
use yii\rest\ActiveController;
use yii\web\Response;
use app\modules\v1\models\MerchantWeixin;
use app\modules\v1\models\MerchantBillDetails;
use app\modules\v1\models\Merchant;

class ReceivelistController extends ActiveController
{
	
    public $modelClass = 'app\modules\v1\models\MerchantBillDetails';
    
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionCreate()
    {
    	$res = array();
    	if(Yii::$app->request->isPost){
    		$openid  = isset(Yii::$app->request->post()['openid'])?Yii::$app->request->post()['openid']:'';
    		if(empty($openid)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '请取消关注后重新关注公众号。';
    			$response		 	= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		$weixininfo  	= MerchantWeixin::findByOpenid($openid);
    		$billlist    	= array();
    		$xyf_total		= '0';
    		$top_text		= Yii::$app->params['top_text'];
    		$totalpage		= 1;
    		if(!empty($weixininfo)){
    			$merchant_id = $weixininfo->merchant_id;
    			$page 		= isset(Yii::$app->request->post()['page'])?Yii::$app->request->post()['page']:1;
    			$pagesize 	= isset(Yii::$app->request->post()['pagesize'])?Yii::$app->request->post()['pagesize']:8;
    			$bill_type 	= isset(Yii::$app->request->post()['bill_type'])?Yii::$app->request->post()['bill_type']:3;
    			if($bill_type == '3'){//信用付
    				$query = MerchantBillDetails::find()
    					->where(['merchant_id'=>$merchant_id,'bill_type'=>'1','pay_type' =>[3,4]])
    					->orderBy(['id' => SORT_DESC]);
    				$countQuery = clone $query;
    				$offset		= $pagesize*($page-1);
    				$models = $query->offset($offset)->limit($pagesize)->all();
    				$arr = array();
    				foreach ($models as $value){
    					$arr['bill_total']  = (string)$value->money;
    					if($value->pay_type == 3){
    						$arr['bill_name']	= '信用付付款';
    					}else{
    						$arr['bill_name']	= '信用付返还手续费';
    					}
    					$arr['bill_status']	= (string)$value->status;
    					$arr['bill_date']	= !empty($value->date_time)?(string)$value->date_time:'';
    					$billlist[] = $arr;
    				}
    				$totalpage	= intval($countQuery->count()/$pagesize)+1;
    				$merchant1 	= Merchant::findOne($merchant_id);
    				if(!empty($merchant1)){
    					$xyf_total  = !empty($merchant1->account_free_balance)?$merchant1->account_free_balance:'0';
    				}
    			}elseif ($bill_type == '1'){//微信
    				$query = MerchantBillDetails::find()
    					->where(['merchant_id'=>$merchant_id,'bill_type'=>'1','pay_type' =>'1'])
    					->orderBy(['id' => SORT_DESC]);
    				$countQuery = clone $query;
    				$offset		= $pagesize*($page-1);
    				$models = $query->offset($offset)->limit($pagesize)->all();
    				$arr = array();
    				foreach ($models as $value){
    					$arr['bill_total']  = (string)$value->money;
    					$arr['bill_name']	= '微信付款';
    					$arr['bill_status']	= (string)$value->status;
    					$arr['bill_date']	= !empty($value->date_time)?(string)$value->date_time:'';
    					$billlist[] = $arr;
    				}
    				$totalpage	= intval($countQuery->count()/$pagesize)+1;
    			}elseif ($bill_type == '2'){//支付宝
    				$query = MerchantBillDetails::find()
    					->where(['merchant_id'=>$merchant_id,'bill_type'=>'1','pay_type' =>'2'])
    					->orderBy(['id' => SORT_DESC]);
    				$countQuery = clone $query;
    				$offset		= $pagesize*($page-1);
    				$models = $query->offset($offset)->limit($pagesize)->all();
    				$arr = array();
    				foreach ($models as $value){
    					$arr['bill_total']  = (string)$value->money;
    					$arr['bill_name']	= '支付宝付款';
    					$arr['bill_status']	= (string)$value->status;
    					$arr['bill_date']	= !empty($value->date_time)?(string)$value->date_time:'';
    					$billlist[] = $arr;
    				}
    				$totalpage	= intval($countQuery->count()/$pagesize)+1;
    			}
    		}
    		$res['data'] = [
					    		'openid'  	=> (string)$openid,
					    		'top_text'	=> (string)$top_text,
					    		'xyf_total'	=> (string)$xyf_total,
					    		'billlist'  => $billlist,
					    		'totalpage' => (string)$totalpage,
					    	];
    		$res['code'] = '10000';
    		$res['msg']  = '成功';
    		$response=Yii::$app->response;
    		$response->format=Response::FORMAT_JSON;
    		$response->data=$res;
    		return $response;
    	}else{
    		$res['code'] = '10402';
    		$res['msg']  = '请取消关注后重新关注公众号。';
    		$response=Yii::$app->response;
    		$response->format=Response::FORMAT_JSON;
    		$response->data=$res;
    		return $response;
    	}  
    }
}